import copy
import re

from docx.enum.text import WD_COLOR_INDEX
from docx.shared import RGBColor
from docx import Document

doc = Document(r'C:\Users\Dapan1\Desktop\助理\方剂学\方剂学笔记.docx')


def highlight_word(document, word_to_highlight):
    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            if word_to_highlight in run.text:
                new_text = run.text.replace(word_to_highlight, '')
                print(new_text, word_to_highlight)
                run.bold = True
                run.font.color.rgb = RGBColor(255, 0, 0)
                run.text = new_text + word_to_highlight





# 打印所有段落的文本
list_ = []
num = 1  # 四级标题顺序
# 主药标题级别
masterStyle = "Heading 3"

# isDelXianDai = False  # 是否删除 【现代研究】
# isDelFuYao = False  # 是否删除附药
# isYaoWuJieShao = False  # 是否是药物介绍
# isDelKongGe = False  # 是否是空格
beforePara = None
beforeHeading = None
for para in doc.paragraphs:
    print(para.style.name, para.text)
    # # 若上一个是【歌诀】 这一个就加个【药物】
    # if beforeHeading and para.style.name == "Body Text":
    #     para._p.getparent().remove(para._p)
    #     continue
    if para.style.name in ["Body Text", "Normal"] and para.text.strip().split('】')[0] in ["【鉴别", "【方解", "【医案举例", "【用法", "【运用", "【方论选录"]:
        print(para.text.strip().split('】')[0])
        para._p.getparent().remove(para._p)
        continue
    # # 删除杂项
    # if para.style.name not in ['Heading 1', 'Heading 2', 'Heading 3'] and para.text.strip().split('】')[0] not in ['【药物', '【歌诀']:
    #     para._p.getparent().remove(para._p)
    #     beforePara = para
    #     continue

    #
    # if para.style.name not in ['Heading 1', 'Heading 2']:
    #     para._p.getparent().remove(para._p)

    # 修改药物 统一格式
    # if para.text.strip().startswith("【歌诀】"):
    #     # print(para.text)
    #     # para.style = masterStyle
    #     # print(para.text)
    #     for run in para.runs:
    #         run.font.color.theme_color = WD_COLOR_INDEX.BLACK
    #         run.font.highlight_color = WD_COLOR_INDEX.AUTO
    #     # run = para.runs[0]
    #     # run.font.color.rgb = RGBColor(255, 0, 0)
    if para.style.name.startswith("Heading"):
        beforeHeading = para
    beforePara = para





doc.save(r'C:\Users\Dapan1\Desktop\助理\方剂学\方剂学笔记2.docx')